#include <bits/stdc++.h>
// 2025/01/20
// tag: Trie
// Author: Zhang Muen
using namespace std;

struct node
{
    char v;
    int cnt;
    unordered_map<char, node *> next;
    node(char a)
    {
        v = a, cnt = 0;
    }
};

int n;
string virus;
queue<node *> qaq;

void dfs(node *q, unordered_set<node *> &visited)
{
    if (q == nullptr || visited.count(q))
        return;
    visited.insert(q);
    qaq.push(q);
    for (auto i : q->next)
        dfs(i.second, visited);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    node *tree = new node(' ');
    node *q = tree;
    string s;
    cin >> virus;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        q = tree;
        for (int i = 0; i < s.size(); i++)
        {
            if (q->next[s[i]])
                q = q->next[s[i]];
            else
                q->next[s[i]] = new node(s[i]), q = q->next[s[i]];
        }
        q->cnt++;
    }
    qaq.push(tree);
    for (auto i : virus)
    {
        int size = qaq.size();
        if (i == '?')
        {
            for (int j = 0; j < size; j++)
            {
                node *temp = qaq.front();
                qaq.pop();
                if (temp == nullptr)
                    continue;
                for (auto k : temp->next)
                    qaq.push(k.second);
            }
        }
        else if (i == '*')
        {
            unordered_set<node *> visited;
            for (int j = 0; j < size; j++)
            {
                node *temp = qaq.front();
                qaq.pop();
                if (temp == nullptr)
                    continue;
                dfs(temp, visited);
            }
        }
        else
        {
            for (int j = 0; j < size; j++)
            {
                node *temp = qaq.front();
                qaq.pop();
                if (temp == nullptr)
                    continue;
                if (temp->next[i])
                    qaq.push(temp->next[i]);
            }
        }
    }
    int ans = 0;
    while (!qaq.empty())
    {
        node *temp = qaq.front();
        qaq.pop();
        if (temp == nullptr)
            continue;
        ans += temp->cnt;
    }
    cout << n - ans << endl;
    return 0;
}